package com.netbliz.common.util;

import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.netbliz.ftpmanagment.pojo.SysParam;

public class MailUtil {
	public static boolean sentMail(SysParam mailSysParam,String mailFrom,List<String> mailTos,String subject,String content){
		if(mailTos==null || mailTos.size()<1){
			return true;
		}
		//建立邮件会话
		Properties property = new Properties();
		property.put("mail.smtp.host", mailSysParam.getV1()); // 存储发送邮件服务器信息
		property.put("mail.smtp.auth", "true"); // 同时通过验证
		try{
			Session mailSession = Session.getInstance(property); 
	        mailSession.setDebug(true);
	        MimeMessage message = new MimeMessage(mailSession);
	        //设置发件人
	        message.setFrom(new InternetAddress(mailSysParam.getV2()));
	        
	        //设置收件人邮箱
	        InternetAddress[] to_mails = new InternetAddress[mailTos.size()];
	        for(int i=0; i<mailTos.size();i++){
	        	to_mails[i] = new InternetAddress(mailTos.get(i));
	        }
	        message.setRecipients(Message.RecipientType.TO ,to_mails);
	      //设置主题 
	        message.setSubject(subject);
	        
	        //设置内容及ContentType和编码方式
	        message.setContent(content, "text/html;charset=utf-8");
	        
	        //设置发送时间
	        message.setSentDate(new Date());
	        
	        //存储信件信息
	        message.saveChanges(); 
	        
	        //创建一个Transport对象
	        Transport transport = mailSession.getTransport("smtp"); 
	        //连接SMTP服务器
	        transport.connect(mailSysParam.getV1(), mailSysParam.getV3(), mailSysParam.getV4());
	        
	        //发送邮件
	        transport.sendMessage(message, message.getAllRecipients()); 
	        transport.close();
		}catch (Exception e) {
			return false;
		}
		return true;
	}
}
